/**
 *@description:中信银行信用卡，抓取流程包装处理
 *@author: LiTao
 *@date:2018/8/14
 */

/**
 * @description:中信信用卡抓取、解析主流程
 */
function credit_crawl_main(){

    //抓取登录成功后的首页
    //citic_credit_handle.crawlIndex();

    //抓取卡列表
    //citic_credit_handle.crawlCardInfo();

    //根据上下文的卡数量，初始化对应的封装对象，供全局使用
    createData();

    //根据首页及卡列表，填充用户基础信息
    citic_credit_handle.parseBasicUserInfo();

    //抓取已出账单
    citic_credit_handle.crawlDONE();

    //解析已出账单
    citic_credit_handle.parseDONE();

    //抓取未出账单
    citic_credit_handle.crawlUONE();

    //解析未出账单
    citic_credit_handle.parseUNDO();

}





/*********************************封装不同业务数据的抓取、解析****************************************/
var citic_credit_handle={

    /**
     * @scription:抓取登录成功后的首页
     */
    crawlIndex:function(){
        logger.info("中信信用卡抓取登录首页开始")
        var indexPage = luaFuncs.zx_get_html();
        storeDataToFile(context,indexPage, "indexPage.html");
        context.put(keys.INDEX_PAGE,indexPage);
    },

    /**
     * @description:抓取卡列表
     */
    crawlCardInfo:function(){
        try {
            logger.info("中信信用卡抓取卡列表开始")
            //获取卡列表页面
            var cardHtml = ajax_post(citic_urls.CARDINFO_URL);
            //本地数据获取慢
            Thread.sleep(1000);
            storeDataToFile(context, cardHtml, "cardList.html");
            if (cardHtml.indexOf("您还未绑卡") != -1 || cardHtml.indexOf("cardlist") == -1){
                var errorMsg = "您还未绑卡，暂不支持业务办理"
                throwBankExceptionByErrorMsg(errorMsg);
            }
            context.put(keys.CARD_INFO, cardHtml);

            var doc = Jsoup.parse(cardHtml);
            var cardList = doc.select("cardlist");

            logger.info("信用卡cardList.size==="+cardList.size());
            //卡数量
            context.put(keys.CRED_CARD_SIZE, cardList.size());
            //卡列表
            context.put(keys.CARD_LIST, cardList);
        } catch (e) {
            logger.error("中信信用卡抓取卡列表异常,行【"+e.lineNumber+"】异常信息【"+e.message+"】");
            statCaughtExceptionByJS(context,e);
        }
    },


    /**
     * 根据首页及卡列表，填充用户基础信息
     * @param data_list
    */
    parseBasicUserInfo:function(){
        try {
            logger.info("信用卡根据首页及卡列表，填充用户基础信息开始");

            //获取首页
            var indexPage = context.get(keys.INDEX_PAGE);
            //解析用户名
            var userName = parseUserName(indexPage);

            //从上下文，获取数据对象集合
            var data_list = context.get(ALL_DATA);
            var cardlist = context.get(keys.CARD_LIST);

            //遍历卡列表
            for (var i = 0; i < cardlist.size(); i++) {
                //卡对象集合中获取每个卡对象
                var creditCardAllData = data_list.get(i);

                var fullCardNum = cardlist.get(i).attr("card_nbr_no");
                var balance = cardlist.get(i).attr("avail_credit_b");
                var cash_balance = cardlist.get(i).attr("avail_cash_b");

                creditCardAllData.setBalance(new BigDecimal(balance.replaceAll(",", "")));
                creditCardAllData.setFullCardNum(fullCardNum);
                creditCardAllData.setBankName("中信银行");
                creditCardAllData.setCardId(UUID.randomUUID().toString());
                creditCardAllData.setCardNum(fullCardNum.substring(fullCardNum.length() - 4, (fullCardNum.length())));
                creditCardAllData.setCardType("信用卡");
                creditCardAllData.setCashBalance(new BigDecimal(cash_balance.replaceAll(",", "")));
                creditCardAllData.setCreditLimit(null);
                creditCardAllData.setCashLimit(null);
                creditCardAllData.setNameOnCard(userName);
            }
            return;
        } catch (e) {
            logger.error("中信信用卡根据首页及卡列表，填充用户基础信息异常,行【"+e.lineNumber+"】异常信息【"+e.message+"】");
        }

        /**
         * 解析用户名
         * @param indexPage
         * @returns {string}
         */
        function parseUserName(indexPage) {
            var doc = Jsoup.parse(indexPage);
            var tiles = doc.select("p").select("[class='title']").text().split("，");
            var name_on_card = tiles[1].trim();
            return name_on_card;
        }
    },

    /**
     * 抓取已出账单
     */
    crawlDONE:function(){
        logger.info("中信信用卡，抓取已出账单开始")

        //从上下文抓取卡列表
        var cardList=context.get(keys.CARD_LIST);

        //已出账单map，用来存储信用卡的已出账单页
        var billHtmlMap=new HashMap();

        //抓取每张卡
        for(var j=0;j<cardList.size();j++){

            var postdata = new HashMap ();
            //卡号
            var card_no = cardList.get(j).attr("card_nbr");
            //卡类型
            var crytype = cardList.get(j).attr("crytype1");
            postdata.put("cardNo", card_no);
            postdata.put("count", "12");
            postdata.put("crytype", crytype);
            postdata.put("rowsPage", "10");
            postdata.put("start_pos", "1");
            postdata.put("startpos", "1");
            postdata.put("stmt_date", "");
            //获取已出账单页面
            var billHtml = ajax_post(citic_urls.DONE_URL, postdata);
            storeDataToFile(context,billHtml, j+"_bill_1.html");

            //根据每张卡的账单页，抓取该卡每月已出账单,获取该卡的所有已出账单列表
            var billEveryMonthList=crawlDoneEveryMonthList(card_no,crytype,billHtml);

            //依据卡号,添加卡的已出账单页
            billHtmlMap.put(card_no,billEveryMonthList);
        }
        //添加到上下文
        context.put(keys.DONE_MAP,billHtmlMap);
        return;

        /**
         * 根据卡账单页，抓取每月已出账单,返回集合列表
         * @param card_no 卡号
         * @param crytype 卡类型
         * @param billHtml 卡账单页
         */
        function crawlDoneEveryMonthList(card_no,crytype,billHtml){
            //获取月份列表

            //例如： <billsMonthList bills_month="2018年07月" stmt_date="20180717"/>
            var billsList = Jsoup.parse(billHtml).select("billsMonthList");

            //月账单列表，用于存放每月的账单
            var billEveryMonthList=new ArrayList();

            //遍历所有月份列表
            for(var i=0;i<billsList.size();i++){
                logger.info("月份数量=="+billsList.size());
                var billinfo = billsList.get(i);
                var bills_month = billinfo.attr("stmt_date");
                var postdatas = new HashMap();
                postdatas.put("cardNo", card_no);
                postdatas.put("count", "12");
                postdatas.put("crytype", crytype);
                postdatas.put("rowsPage", "10");
                postdatas.put("start_pos", "1");
                postdatas.put("startpos", "1");
                postdatas.put("stmt_date", bills_month);
                var billdetailHtml = ajax_post(citic_urls.DONE_URL, postdatas);
                storeDataToFile(context,billdetailHtml, j+"_bills" + bills_month + "_1.html");

                //添加月账单列表
                billEveryMonthList.add(billdetailHtml);
            }
            //返回月账单列表
            return billEveryMonthList;
        }
    },




    /**
     * 解析已出账单
     */
    parseDONE:function(){
        try {
            logger.info("中信信用卡，解析已出账单开始");

            //从上下文，获取已出账单map
            var doneMap = context.get(keys.DONE_MAP);

            //从上下文，获取数据对象集合
            var data_list = context.get(ALL_DATA);

            //从上下文抓取卡列表
            var cardList=context.get(keys.CARD_LIST);

            //遍历每个卡
            for (var i = 0; i<cardList.size(); i++) {
                //卡号
                var card_no = cardList.get(i).attr("card_nbr");

                //依据卡号，获取每个卡的月账单列表
                var billEveryMonthList = doneMap.get(card_no);

                //获取每个卡对应的数据对象
                var creditCardAllData = data_list.get(i);

                //获取每个卡对应的月账单集合
                var billList = creditCardAllData.getBills();

                //舍弃掉creditCardAllData初始时创建的对象内容,因其中有用的数据很少
                billList.clear();

                //遍历每个月
                for (var j = 0;j < billEveryMonthList.size(); j++) {

                    //获取每个月的账单详情
                    var billdetailHtml = billEveryMonthList.get(j);

                    //解析已出账单详情页面
                    parseBillDetailInfo(j,billdetailHtml, billList);
                }
            }
        } catch (e) {
            logger.error("中信信用卡，解析已出账单异常,行【"+e.lineNumber+"】异常信息【"+e.message+"】");
        }
    },

    /**
     * 抓取未出账单
     */
    crawlUONE:function(){
        logger.info("中信信用卡，抓取未出账单开始");
        //从上下文，获取取卡列表
        var cardList=context.get(keys.CARD_LIST);

        //未出账单map.存放抓取的未出账单
        var undoMap=new HashMap( );

        //遍历卡列表
        for(var i=0;i<cardList.size();i++){
            //卡号
            var card_no = cardList.get(i).attr("card_nbr");
            //卡类型
            var crytype = cardList.get(i).attr("crytype1");
            var postdata = new HashMap();
            postdata.put("cardNo", card_no);
            postdata.put("crytype", crytype);
            postdata.put("rowsPage", "10");
            postdata.put("startpos", "1");
            postdata.put("stmt_date", "");
            //获取未出账单页面
            var unbilldetailHtml = ajax_post(citic_urls.UNDO_URL, postdata);
            storeDataToFile(context,unbilldetailHtml, i+"_undonepage_1.html");
            //添加未出账单
            undoMap.put(i,unbilldetailHtml);
        }
        //加入上下文
        context.put(keys.UNDO_MAP,undoMap);
    },

    /**
     * 解析未出账单
     */
    parseUNDO:function(){

        try {
            logger.info("中信信用卡，解析未出账单开始")
            //从上下文，获取未出账单map
            var undoMap = context.get(keys.UNDO_MAP);
            //从上下文，获取数据对象集合
            var data_list = context.get(ALL_DATA);

            for (var i = 0; i < undoMap.size(); i++) {
                var unbilldetailHtml = undoMap.get(i);
                //获取每个卡对应的数据对象
                var creditCardAllData = data_list.get(i);
                //获取每个卡的账单集合
                var billList = creditCardAllData.getBills();
                logger.info("undoMap.size()==" + undoMap.size());
                //解析未出账单
                parseUnbillDetailInfo(unbilldetailHtml,billList);
            }
        } catch (e) {
            logger.error("中信信用卡，解析未出账单异常",e);
            statCaughtExceptionByJS(context,e);
        }
    }
}





/***************************************辅助型业务变量和方法******************************************/
var citic_urls={
    //卡列表
    CARDINFO_URL:"https://creditcard.ecitic.com/citiccard/newonline/common.do?func=querySignCards",
    //已出账单
    DONE_URL:"https://creditcard.ecitic.com/citiccard/newonline/billQuery.do?func=queryBillInfo",
    //未出账单
    UNDO_URL:"https://creditcard.ecitic.com/citiccard/newonline/billQuery.do?func=queryUnSettleBill"
}



/**
 * 解析已出账单的每月账单详情
 * @param countNo   解析次数
 * @param billdetailHtml    已出账单详情页
 * @param billList  集合列表
 */
function parseBillDetailInfo(countNo,billdetailHtml,billList) {
        var doc = Jsoup.parse(billdetailHtml);
        var bill_monthss = doc.select("billProfile").first();
        if (bill_monthss == null) {
            logger.info("第" + countNo + "次解析账单月份为null,停止解析该月已出账单，继续抓取下一个月");
            return;
        }

        //创建账单实例
        var billAllData = new BillAllData();

        //解析账单表头数据
        parseDoneBillBaseData(doc,billAllData);

        //购物详情列表
        var shoppingList = doc.select("billDetailList");

        //编号
        var orderIndex = 1;

        //购物账单明细列表
        var shopping_sheets = new ArrayList();
        //分期记录
        var installments = new ArrayList();

        //解析购物账单明细\分期明细
        parseShoppingAndInstall(orderIndex,shoppingList,shopping_sheets,installments);


        //设置购物明细
        billAllData.setShoppingSheets(shopping_sheets);

        //设置分期明细
        billAllData.setInstallments(installments);

        //添加账单对象
        billList.add(billAllData);
}


/**
 * 解析已出账单表头数据
 * @param doc
 * @param billAllData
 */
function parseDoneBillBaseData(doc,billAllData){
    try { //账单月
        var bill_month = doc.select("billProfile").first().attr("bills_month");
        //账单日
        var bill_date = doc.select("billProfile").first().attr("stmt_date");
        //还款日
        var payment_due_date = doc.select("billProfile").first().attr("dte_pymt_due");
        //本期账单金额
        var new_balance = doc.select("billProfile").first().attr("cur_amt").replaceAll(",", "");

        //已出账单，本期账单金额,供后续未出账单使用
        if (StringUtils.isBlank(context.get(keys.DONE_NEWBALANCE))) {
            context.put(keys.DONE_NEWBALANCE, new_balance);
        }
        //最低还款额
        var min_payment = doc.select("billProfile").first().attr("min_pay").replaceAll(",", "");
        //上期账单金额
        var last_balance = doc.select("billProfile").first().attr("pre_pay").replaceAll(",", "");
        //上期还款金额
        var last_payment = doc.select("billProfile").first().attr("pre_hpay").replaceAll(",", "");
        //本期新增款额
        var new_charges = doc.select("billProfile").first().attr("bq_xz").replaceAll(",", "");


        billAllData.setBillId(UUID.randomUUID().toString());
        billAllData.setBillType("DONE");
        billAllData.setBankName("中信银行");
        var strBuilderMonth = new StringBuilder(bill_month);
        bill_month = strBuilderMonth.insert(4, "-").toString();
        billAllData.setBillMonth(bill_month);
        var strBuildDate = new StringBuilder(bill_date);
        bill_date = strBuildDate.insert(4, "-").insert(7, "-").toString();
        billAllData.setBillDate(yyyy_MM_dd_PARSER.parse(bill_date));
        payment_due_date = payment_due_date.replaceAll("年", "-")
            .replaceAll("月", "-").replaceAll("日", "");
        if (payment_due_date !== null && payment_due_date.length !== 0) {
            billAllData.setPaymentDueDate(yyyy_MM_dd_PARSER.parse(payment_due_date));
        }
        billAllData.setInterest(null);
        billAllData.setNewBalance(new BigDecimal(new_balance));
        billAllData.setMinPayment(new BigDecimal(min_payment));
        billAllData.setLastBalance(new BigDecimal(last_balance));
        billAllData.setLastPayment(new BigDecimal(last_payment).negate());
        billAllData.setNewCharges(new BigDecimal(new_charges));
        billAllData.setCreditLimit(null);
        billAllData.setCashLimit(null);
        billAllData.setUsdCashLimit(null);
        billAllData.setUsdCreditLimit(null);
        billAllData.setUsdInterest(null);
        billAllData.setUsdLastBalance(null);
        billAllData.setUsdLastPayment(null);
        billAllData.setUsdMinPayment(null);
        billAllData.setUsdNewCharges(null);
        billAllData.setUsdNewBalance(null);
    } catch (e) {
        logger.error("中信信用卡已出账单表头解析异常,行【"+e.lineNumber+"】异常信息【"+e.message+"】");
    }
}


/**
 * 解析购物账单明细（已出、未出 共用）
 * @param shoppingList  购物列表
 */
// function parseShoppingSheets(shoppingList){
//     //编号
//     var orderIndex = 1;
//
//     //购物账单明细列表
//     var shopping_sheets = new ArrayList();// 消费记录
//
//     try { //遍历购物详情列表
//         for (var i = 0; i < shoppingList.size(); i++) {
//             //购物详情
//             var shopDetail = shoppingList.get(i);
//
//             //解析数据
//             var description = shopDetail.attr("tran_desc");
//             var remark = shopDetail.attr("tram_code");
//             var amount_money = shopDetail.attr("tran_amt").replaceAll(",", "");
//             var trans_date = shopDetail.attr("tran_date");
//             var post_date = shopDetail.attr("post_date");
//             var card_4_num = shopDetail.attr("card_4_num");
//
//             //账单详情实例
//             var expense = new Expense();
//             expense.setId(UUID.randomUUID().toString());
//             expense.setBalance(null);
//             expense.setRemark(remark);
//             expense.setDescription(description.replace(" ", ""));
//             expense.setAmountMoney(new BigDecimal(amount_money));
//             expense.setCategory(classify(description, expense.getAmountMoney()));
//             expense.setCardNum(card_4_num);
//             //详见  imports.js 中的静态导入，因此yyyy_MM_dd_HH_mm_ss_PARSER 可以使用
//             expense.setTransDate(yyyy_MM_dd_HH_mm_ss_PARSER.parse(trans_date + " 00:00:00"));
//             expense.setTransAddr("");
//             expense.setTransMethod("");
//             expense.setTransChannel("");
//             expense.setOrderIndex(orderIndex++);
//             expense.setCurrencyType("CNY");
//             expense.setOppositeBank("");
//             expense.setOppositeCardNo("");
//             expense.setNameOnOppositeCard("");
//             expense.setPostDate(yyyy_MM_dd_PARSER.parse(post_date));
//
//             //添加
//             shopping_sheets.add(expense);
//         }
//     } catch (e) {
//         logger.error("中信信用卡已出账单购物明细解析异常,行【"+e.lineNumber+"】异常信息【"+e.message+"】");
//     }
//     return shopping_sheets;
// }

/**
 * 解析已出账单,购物账单明细
 * @param shoppingList  购物列表
 */
function parseShoppingAndInstall(orderIndex,doc,shopping_sheets,installments){

    //遍历购物详情列表
    try {
        var shoppingList = doc.select("billDetailList");
        var shoppingList1 = doc.select("billDetailList");
        for (var i = 0; i < shoppingList.size(); i++) {
            //购物详情
            var shopDetail = shoppingList.get(i);

            //解析数据
            var description = shopDetail.attr("tran_desc");
            var remark = shopDetail.attr("tram_code");
            var amount_money = shopDetail.attr("tran_amt").replaceAll(",", "");
            var trans_date = shopDetail.attr("tran_date");
            var post_date = shopDetail.attr("post_date");
            var card_4_num = shopDetail.attr("card_4_num");

            //账单详情实例
            var expense = new Expense();
            expense.setId(UUID.randomUUID().toString());
            expense.setBalance(null);
            expense.setRemark(remark);
            expense.setDescription(description.replace(" ", ""));
            expense.setAmountMoney(new BigDecimal(amount_money));
            expense.setCategory(classify(description, expense.getAmountMoney()));
            expense.setCardNum(card_4_num);
            //详见  imports.js 中的静态导入，因此yyyy_MM_dd_HH_mm_ss_PARSER 可以使用
            expense.setTransDate(yyyy_MM_dd_HH_mm_ss_PARSER.parse(trans_date + " 00:00:00"));
            expense.setTransAddr("");
            expense.setTransMethod("");
            expense.setTransChannel("");
            expense.setOrderIndex(orderIndex++);
            expense.setCurrencyType("CNY");
            expense.setOppositeBank("");
            expense.setOppositeCardNo("");
            expense.setNameOnOppositeCard("");
            expense.setPostDate(yyyy_MM_dd_PARSER.parse(post_date));

            //添加
            shopping_sheets.add(expense);

            //购物详情
            var shopDetail = shoppingList.get(i);

            //购物描述
            var description = shopDetail.attr("tran_desc");

            var result = getGroup0("\\s*(\\d{1,3}/\\d{1,3})", description);
            //分期值不为空
            if (result!=null&&result!="" && !description.contains("手续费")) {
                //分期实例
                var installment = new Installment();
                var termsArr = result.split("/");
                if (termsArr.length == 2) {
                    var dqTerm = Integer.parseInt(termsArr[0]);
                    var totalTerm = Integer.parseInt(termsArr[1]);
                    installment.setCurrencyType("CNY");
                    installment.setCurrentMonth(dqTerm);

                    var installment_type = "CONSUME";
                    if (description.indexOf("账单") !== -1) {
                        installment_type = "BILL";
                    } else if (description.indexOf("现金") !== -1 || description.indexOf("圆梦金") !== -1) {
                        installment_type = "CASH";
                    }
                    installment.setInstallmentType(installment_type);
                    installment.setPostDate(yyyy_MM_dd_PARSER.parse(post_date));
                    installment.setShoppingsheetId(UUID.randomUUID().toString());
                    installment.setTotalMonth(totalTerm);
                    if (trans_date != null) {
                        trans_date = trans_date + " 00:00:00";
                    }
                    installment.setTransDate(yyyy_MM_dd_PARSER.parse(trans_date));
                    installment.setAmountMoney(new BigDecimal(amount_money));
                    installment.setInstallmentDesc(description.replace(" ", ""));
                    installment.setTransferFee(null);
                    for (var k = 0; k < shoppingList1.size(); k++) {
                        var shoppDetail = shoppingList1.get(k);
                        var description1 = shoppDetail.attr("tran_desc");
                        var amount_money1 = shoppDetail.attr("tran_amt").replaceAll(",", "");
                        if (description1.indexOf("手续费") !== -1) {
                            //设置手续费
                            installment.setHandingFee(new BigDecimal(amount_money1));
                            installment.setHandingfeeDesc(description1.replace(" ", ""));
                            shoppingList1.remove(k);
                            break;
                        }
                    }
                }
                //添加分期
                installments.add(installment);
            }
        }
    } catch (e) {
        logger.error("中信信用卡已出账单购物明细及分期解析异常,行【"+e.lineNumber+"】异常信息【"+e.message+"】");
    }
}







/**
 * 解析已出账单,分期明细
 * @param shoppingList  购物列表
 */
function parseDoneInstallments(shoppingList){
    return parseInstallments(shoppingList);
}

/**
 * 解析分期明细（已出账单、未出账单 共用）
 * @param shoppingList  购物列表
 */
function parseInstallments(shoppingList){
    //分期记录
    var installments = new ArrayList();
    //遍历购物详情列表
    try {
        for (var i = 0; i < shoppingList.size(); i++) {
            //购物详情
            var shopDetail = shoppingList.get(i);

            //购物描述
            var description = shopDetail.attr("tran_desc");

            var result=getGroup0("\\s*(\\d{1,3}/\\d{1,3})",description);
            //分期值不为空
            if (result!= "" && !description.contains("手续费")) {
                //分期实例
                var installment = new Installment();
                var termsArr = result.split("/");
                if (termsArr.length == 2) {
                    var dqTerm = Integer.parseInt(termsArr[0]);
                    var totalTerm = Integer.parseInt(termsArr[1]);
                    installment.setCurrencyType("CNY");
                    installment.setCurrentMonth(dqTerm);

                    var installment_type = "CONSUME";
                    if (description.indexOf("账单") !== -1) {
                        installment_type = "BILL";
                    } else if (description.indexOf("现金") !== -1 || description.indexOf("圆梦金") !== -1) {
                        installment_type = "CASH";
                    }
                    installment.setInstallmentType(installment_type);
                    installment.setPostDate(yyyy_MM_dd_PARSER.parse(post_date));
                    installment.setShoppingsheetId(UUID.randomUUID().toString());
                    installment.setTotalMonth(totalTerm);
                    if (trans_date != null) {
                        trans_date = trans_date + " 00:00:00";
                    }
                    installment.setTransDate(yyyy_MM_dd_PARSER.parse(trans_date));
                    installment.setAmountMoney(new BigDecimal(amount_money));
                    installment.setInstallmentDesc(description.replace(" ", ""));
                    installment.setTransferFee(null);
                    for (var k = 0; k < shoppingList.size(); k++) {
                        var shoppDetail = shoppingList.get(k);
                        var description1 = shoppDetail.attr("tran_desc");
                        var amount_money1 = shoppDetail.attr("tran_amt").replaceAll(",", "");
                        if (description1.indexOf("手续费") !== -1) {
                            //设置手续费
                            installment.setHandingFee(new BigDecimal(amount_money1));
                            installment.setHandingfeeDesc(description1.replace(" ", ""));
                            shoppingList.remove(k);
                            break;
                        }
                    }
                }
                //添加分期
                installments.add(installment);
            }
        }
    } catch (e) {
        logger.error("中信信用卡已出账单install解析异常,行【"+e.lineNumber+"】异常信息【"+e.message+"】");
    }
    //循环结束，返回分期列表
    return installments;
}




/**
 * 解析未出账单详情
 * @param unbilldetailHtml
 * @param bills
 */
function parseUnbillDetailInfo( unbilldetailHtml,billList){
    var doc = Jsoup.parse(unbilldetailHtml);

    //账单详情列表
    var shoppingList = doc.select("billDetailList");

    //账单实例
    var billAllData = new BillAllData();

    //解析未出账单,表头数据
    parseUndoBaseData(unbilldetailHtml,billAllData,shoppingList);

    //编号
    var orderIndex = 1;

    //购物账单明细列表
    var shopping_sheets = new ArrayList();
    //分期记录
    var installments = new ArrayList();

    //解析购物账单明细\分期明细
    parseShoppingAndInstall(orderIndex,doc,shopping_sheets,installments);

    //添加购物账单明细
    billAllData.setShoppingSheets(shopping_sheets);

    //添加账单明细
    billAllData.setInstallments(installments);

    billList.add(billAllData);
}

/**
 * 解析未出账单,表头数据
 * @param unbilldetailHtml
 * @param billAllData
 * @param shoppingList
 */
function parseUndoBaseData(unbilldetailHtml,billAllData,shoppingList){
    var head = getGroupByIndex("<head.*?/>", unbilldetailHtml, 0)
    var bill_date = getGroupByIndex("\\d{4}-\\d{2}-\\d{2}", head, 0)

    //账单日
    var date = yyyy_MM_dd_PARSER.parse(bill_date);
    var rightNow = Calendar.getInstance();
    rightNow.setTime(date);
    rightNow.add(Calendar.MONTH, 1);// 日期加1月
    var rightNowTime = rightNow.getTime();

    //账单月
    var billMonth = yyyy_MM_dd_PARSER.format(rightNowTime);

    //特殊账单月处理
    if(equals("0000-00-00", billMonth)){
        var calendar =Calendar.getInstance();
        var year = calendar.get(Calendar.YEAR);
        var month=calendar.get(calendar.MONTH)+1;
        var monthInt=Integer.parseInt(month);
        if(monthInt<10){
            month="0"+month;
        }
        billMonth = year + "-" + month + "-01";
    }

    billAllData.setBillId( UUID.randomUUID().toString());
    billAllData.setBillType("UNDONE");
    billAllData.setBankName("中信银行");
    billAllData.setBillDate(yyyy_MM_dd_PARSER.parse(billMonth));
    billAllData.setBillMonth(billMonth.substring(0, 7));
    billAllData.setCreditLimit(null);
    billAllData.setCashLimit(null);
    billAllData.setUsdCashLimit(null);
    billAllData.setUsdCreditLimit(null);
    billAllData.setUsdInterest(null);
    billAllData.setUsdLastBalance(null);
    billAllData.setUsdLastPayment(null);
    billAllData.setUsdMinPayment(null);
    billAllData.setUsdNewCharges(null);
    billAllData.setUsdNewBalance(null);
    billAllData.setInterest(null);
    billAllData.setMinPayment(null);
    billAllData.setLastPayment(null);

    //本期新增款额 初始为0
    var new_charge=new BigDecimal("0");
    //本期账单金额    初始为0
    var new_balance=new BigDecimal("0");
    //遍历购物详情列表,计算new_charge
    for(var i=0;i<shoppingList.size();i++){
        //购物详情
        var shopDetail = shoppingList.get(i);
        //解析数据
        var description = shopDetail.attr("tran_desc");
        var remark = shopDetail.attr("tram_code");
        var amount_money = shopDetail.attr("tran_amt").replaceAll(",", "");
        //不包含"还款"
        if(description.indexOf("还款")==-1 && remark.indexOf("还款")==-1){
            //未出账单处理“本期新增款额”与已出账单不一样
            //本期新增款额
            new_charge = new_charge.add(new BigDecimal(amount_money)) ;
            //本期账单金额
            new_balance=new_charge;
        }
    }
    billAllData.setNewCharges(new_charge);

    //从上下文获取上月账单金额
    var last_balance = context.get(keys.DONE_NEWBALANCE);

    var big_last_balance =StringUtils.isBlank(last_balance)?null:new BigDecimal(last_balance);

    //上期账单金额
    billAllData.setLastBalance(StringUtils.isBlank(last_balance)?null:big_last_balance);
    //本期账单金额
    billAllData.setNewBalance(StringUtils.isBlank(last_balance)?new_balance:new BigDecimal(last_balance).add(new_balance));
}

/**
 * 解析未出账单,购物账单明细
 * @param shoppingList  购物列表
 */
function parseUndoShoppingSheets(shoppingList){

    return parseShoppingAndInstall(shoppingList);
}

/**
 * 解析未出账单,分期明细
 * @param shoppingList  购物列表
 */
function parseUndoInstallments(shoppingList){
    return parseInstallments(shoppingList);
}



/**
 * 正则表达式组 多个组
 *
 * @param express
 * @param content
 * @return
 */
function getMatch( express,  content) {
    var matcher=getGroup0(express,content);
    return matcher;
}